home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
C and C++
/
Libraries
/
GUSI
/
Examples
/
GUSITest.c
< prev
next >
Wrap
Text File
|
1993-09-11
|
4KB
|
236 lines
/*********************************************************************
File : GUSI - Grand Unified Socket Interface
File : GUSITest.c - Common testing gear
Author : Matthias Neeracher <neeri@iis.ethz.ch>
Started : 25Jul92 Language : MPW C
08Sep92 MN Factor out more common code
20Sep92 MN Allow empty lines & comments
Last : 20Sep92
*********************************************************************/
#include <Memory.h>
#include <QuickDraw.h>
#include <GUSITest.h>
#include <Types.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <sys/errno.h>
Boolean HellHoundOnMyTrail = true; /* Gotta keep on moving */
char infilename[200];
char * inputfrom;
FILE * input;
int inputline;
CmdDef commands[NROFCMDS];
void Help(char, char, const char *)
{
char ch1,ch2;
printf("Commands are:\n\n");
for (ch1 = 'a'; ch1 <= 'z'; ++ch1)
for (ch2 = 0; ch2 <= 'z'; ch2 ? ++ch2 : (ch2 = 'a'))
if (HELPMSG(ch1,ch2))
printf(
"\t%c%c %-25s -- %s\n",
ch1,
ch2 ? ch2 : ' ',
USAGE(ch1,ch2),
HELPMSG(ch1,ch2));
printf("\n");
}
void Where()
{
if (inputfrom)
printf("File '%s'; Line %d\n", inputfrom, inputline);
}
void Prompt()
{
extern int StandAlone;
if (!inputfrom)
printf("[%d]%c", inputline, StandAlone ? ' ' : '\n');
}
#define CASE(code) case code: return #code
const char * Explain()
{
switch (errno) {
CASE(EPERM);
CASE(ENOENT);
CASE(ESRCH);
CASE(EINTR);
CASE(EIO);
CASE(ENXIO);
CASE(E2BIG);
CASE(ENOEXEC);
CASE(EBADF);
CASE(ECHILD);
CASE(EDEADLK);
CASE(ENOMEM);
CASE(EACCES);
CASE(EFAULT);
CASE(ENOTBLK);
CASE(EBUSY);
CASE(EEXIST);
CASE(EXDEV);
CASE(ENODEV);
CASE(ENOTDIR);
CASE(EISDIR);
CASE(EINVAL);
CASE(ENFILE);
CASE(EMFILE);
CASE(ENOTTY);
CASE(ETXTBSY);
CASE(EFBIG);
CASE(ENOSPC);
CASE(ESPIPE);
CASE(EROFS);
CASE(EMLINK);
CASE(EPIPE);
CASE(EDOM);
CASE(ERANGE);
CASE(EWOULDBLOCK);
CASE(EINPROGRESS);
CASE(EALREADY);
CASE(ENOTSOCK);
CASE(EDESTADDRREQ);
CASE(EMSGSIZE);
CASE(EPROTOTYPE);
CASE(ENOPROTOOPT);
CASE(EPROTONOSUPPORT);
CASE(ESOCKTNOSUPPORT);
CASE(EOPNOTSUPP);
CASE(EPFNOSUPPORT);
CASE(EAFNOSUPPORT);
CASE(EADDRINUSE);
CASE(EADDRNOTAVAIL);
CASE(ENETDOWN);
CASE(ENETUNREACH);
CASE(ENETRESET);
CASE(ECONNABORTED);
CASE(ECONNRESET);
CASE(ENOBUFS);
CASE(EISCONN);
CASE(ENOTCONN);
CASE(ESHUTDOWN);
CASE(ETOOMANYREFS);
CASE(ETIMEDOUT);
CASE(ECONNREFUSED);
CASE(ELOOP);
CASE(ENAMETOOLONG);
CASE(EHOSTDOWN);
CASE(EHOSTUNREACH);
CASE(ENOTEMPTY);
CASE(EPROCLIM);
CASE(EUSERS);
CASE(EDQUOT);
CASE(ESTALE);
CASE(EREMOTE);
CASE(EBADRPC);
CASE(ERPCMISMATCH);
CASE(EPROGUNAVAIL);
CASE(EPROGMISMATCH);
CASE(EPROCUNAVAIL);
CASE(ENOLCK);
CASE(ENOSYS);
default:
return "Unknown";
}
}
void Usage(char ch1, char ch2)
{
printf("# Usage is: %c%c %s\n", ch1, ch2 ? ch2 : ' ', USAGE(ch1,ch2));
Where();
}
void Dispatch(const char * command)
{
char ch1 = command[0];
char ch2 = command[1];
TestCmd exec;
/* We are guaranteed to have at least one valid character */
switch (ch1) {
case '\n':
case '#':
return;
}
if (!ch2)
++command;
else {
if (isspace(ch2)) {
command += 1;
ch2 = 0;
} else
command += 2;
/* Skip rest of first word */
for (; *command && !isspace(*command); ++command);
/* Skip whitespace */
while (isspace(*command))
++command;
}
if (isalpha(ch1) && (!ch2 || isalpha(ch2)) && (exec = DISPATCH(ch1,ch2)))
exec(ch1, ch2, command);
else {
if (ch2)
printf("# Unknown command: '%c%c'\n", ch1, ch2);
else
printf("# Unknown command: '%c'\n", ch1);
printf("# Type 'h' for a list of known commands.\n");
Where();
}
}
void Quit(char, char, const char *)
{
HellHoundOnMyTrail = false;
}
void RunTest(int argc, char ** argv)
{
char cmd[80];
COMMAND('h', 0, Help, "", "Print this list");
COMMAND('q', 0, Quit, "", "End the sad existence of this program");
InitGraf((Ptr) &qd.thePort);
if (!--argc)
Help('h', 0, "");
do {
if (argc && strcmp(inputfrom = *++argv, "-")) {
printf("Executing %s…\n", inputfrom);
input = fopen(inputfrom, "r");
} else {
inputfrom = 0;
input = stdin;
}
inputline = 1;
while (HellHoundOnMyTrail && (Prompt(), fgets(cmd, 80, input))) {
Dispatch(cmd);
++inputline;
}
} while (HellHoundOnMyTrail && --argc > 0);
printf("So long, it's been good to know you.\n");
}